
# Relevant variables ------------------------------------------------------

# IPV

ipv_vars <- c("vaw_action",
							"should_intervene",
							"IPV_discussion") 

# absenteeism

abs_vars <- c("absenteeism_action",
							"repeat_absence",
							"abs_discussion")



# abortion

abo_vars <- c("abo_action",
							"should_help",
							"abo_discussion")



# Relevant functions ------------------------------------------------------


getPVal <- function(outcome, treatment, data){
	results <- ols_main(outcome = outcome,
										treatment = treatment,
										resample_FE = TRUE,
										block_FE = TRUE,
										audience_size = TRUE,
										cluster_SE = TRUE,
										covariates = NULL,
										the_data = data,
										dosage = FALSE,
										dosage_indicator = NULL
	)
	
	pval <- pt(results$fit_summary[treatment, "t value"], results$fit$df,lower.tail = F)
	
	return(pval)
}



getSortedPVals <- function(data, 
														ipv_vars,
														abo_vars,
														abs_vars,
														adjustment = FALSE,
														observed_rank_order = NULL){
	
	pvals_ipv <- sapply(ipv_vars, 
											getPVal, 
											 treatment = "IPV", 
											 data = subset(data, respondent_category == "Complier"))
	
	pvals_abo <- sapply(abo_vars, 
											getPVal, 
											 treatment = "abortion",
											 data = subset(data, respondent_category == "Complier"))
	
	pvals_abs <- sapply(abs_vars, 
											getPVal, 
											 treatment = "absenteeism", 
											 data = subset(data, respondent_category == "Complier"))
	
	
	pvals <- c(pvals_ipv, pvals_abo, pvals_abs)
	
	pvals <- sort(pvals,decreasing = F)
	
	if(adjustment){
		pvals <- pvals[observed_rank_order]
		for(i in 1:length(pvals)) {
			pvals[i] <- min(pvals[i:length(pvals)])
		}
	}
	
	
	return(pvals)

}



# Endline -----------------------------------------------------------------

obs <- getSortedPVals(el,
											 ipv_vars = ipv_vars,
											 abo_vars = abo_vars,
											 abs_vars = abs_vars)


observed_rank_order <- names(obs)


set.seed(12345)

simulations <- replicate(n = sims_mc, expr = {
	new_data <- rerandomize()
	getSortedPVals(new_data,
									ipv_vars = ipv_vars,
									abo_vars = abo_vars,
									abs_vars = abs_vars,
									adjustment = T,
									observed_rank_order = observed_rank_order)},simplify = T)

pvals <- rowMeans(simulations < obs)
names(pvals) <- names(obs)
original <- pvals

for(i in 1:length(pvals)) {
	pvals[i] <- min(pvals[i:length(pvals)])
}



table_data <- data.frame(original_pvals = obs, 
												 adjusted_pvals = pvals)
# DOUBLE CHECK THAT THESE LABELS ARE CORRECT IF YOU CHANGE SOMETHING - ORDER MAY CHANGE. 
rownames(table_data)  <- c("Conative attitudes VAW",
													 "Conative attitudes ABS",
													 "Conative attitudes ABO",
													 "Prescriptive norm VAW",
													 "Prescriptive norm ABS",
													 "Discussion ABO",
													 "Discussion VAW",
													 "Prescriptive norm ABO",
													 "Discussion ABS")

sink("03_tables/pvals_multiple_comparisons.tex")
print(kable(table_data, 
			col.names = c("Unadjusted parametric $p$-value",
																"Adjusted $p$-value"), 
			format = "latex",
			escape = F,
			digits = 3,align = c("c","c")))
sink()

